package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongo.Visitors;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.*;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map count(Long userId) {
        //统计喜欢
        Query query1=Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        long loveCount = mongoTemplate.count(query1, UserLike.class);
        //统计喜欢我的粉丝数
        Query query2=Query.query(Criteria.where("LikeUserId").is(userId).and("isLike").is(true));
        long fanCount = mongoTemplate.count(query2, UserLike.class);

        Map map=new HashMap();
        map.put("loveCount",loveCount);
        map.put("fanCount",fanCount);

        return map;
    }

    //根据视频用户id和登录用户id查询
    @Override
    public UserLike findByid(Long uid, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(uid).and("LikeUserId").is(userId));
        return mongoTemplate.findOne(query, UserLike.class);

    }

    //删除喜欢表中的单条数据
    @Override
    public void delete(Long userId, Long likeUserId, boolean isLike) {
        Query query = Query.query(Criteria.where("userId").is(userId).
                and("LikeUserId").is(likeUserId).
                and("isLike").is(isLike));
        mongoTemplate.remove(query,UserLike.class);

    }

    @Override
    public Map<Long, Boolean> alreadyLove(Long userId, Set<Long> userIds) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").in(userIds));
        List<UserLike> userLikeList = mongoTemplate.find(query,UserLike.class);
        //构造返回的Map
        Map<Long, Boolean> map = new HashMap<>();
        for (UserLike userLike : userLikeList) {
            Long id = userLike.getUserId();
            map.put(id, userLike.getIsLike());
        }
        return map;
    }
}
